<?php
/**
 * 发送短信实现
 * @author Liuyi<liuy@pvc123.com>
 * @created 2016-12-17:03:42
 */
namespace sms\service;

use herosphp\core\Loader;
use herosphp\core\Log;
use sms\service\interfaces\ISmsService;
use utils\sms\Cr6868Provider;

Loader::import('sms.service.interfaces.ISmsService');
Loader::import('utils.sms.Cr6868Provider');
class SmsService implements ISmsService {

    private $user = null;
    private $pass = null;
    private $key = null;
    private $sign = null;
    private $authed = false;
    private $service = null;

    public function __construct()
    {
        $this->user = "13922508685";
        $this->pass = "CE16E3A42A7EDD86F3EB1FA5FE5D";
        $this->key = "2342qasfasfs1234234";
        $this->sign = "塑料报价";
        $this->authed=true;
        $this -> service = new Cr6868Provider();
        $this -> service -> auth($this -> user,$this -> pass, $this -> key, $this ->sign);
    }

    /**
     * 发送短信接口
     * @param $contents ，发送内容
     * @param $mobile ， 手机号码
     * @return mixed
     */
    public function send($contents, $mobile)
    {
        // TODO: Implement send() method.
        $retVo = $this -> service ->send($mobile, $contents, date('Y-m-d H:i:s'));
        if( $retVo===true ){
            Log::info("发送短信成功，手机号码：".$mobile);
            return true;
        }else{
            Log::error("发送短信失败，错误信息：".$retVo);
            return false;
        }

    }

    /**
     * 构建完整的短信内容
     * @param $rule
     * @param $data
     * @return mixed
     */
    public function build($rule,$data)
    {
        // TODO: Implement build() method.
        $template = MT($rule);
        if( !$template ){
            return false;
        }
        //替换默认的变量，如客服电话
        $default_vars = MT('TEMPLATE_DEFAULT_VARS');
        foreach ( $default_vars as $key => $value ){
            $template = str_replace($key,$value,$template);
        }
        foreach ($data as $k => $v){
            $template = str_replace($k,$v,$template);
        }
        return $template;
    }
}